package cn.fbs.service;

import cn.hutool.core.io.IoUtil;
import cn.fbs.dao.dm.*;
import cn.fbs.dao.oracle.QbTjwjqkWqzbMtMapper;
import cn.fbs.model.NewTCodeTargetType;
import cn.fbs.model.TImageInfo;
import cn.fbs.model.TVideoInfo;
import cn.fbs.model.dm.QbTjwjqkWqzbMt;
import cn.fbs.model.dm.ZzbzSQbMtlx;
import cn.fbs.util.Result;
import cn.fbs.vo.ShareTypeAndTarget;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

@Service
public class TripartiteService {


    @Autowired
    private DMImageInfoMapper imageInfoMapper;

    @Autowired
    private DMVideoInfoMapper videoInfoMapper;
    @Autowired
    private QbTjwjqkWqzbMtMapper qbMapper;

    @Autowired
    private ZzbzSQbMtlxMapper mtlxMapper;

    @Autowired
    private ZzbzSQbMtgsMapper mtgsMapper;

    @Autowired
    private DMNewTargetMapper dmNewTargetMapper;

    @Value("${resource.locations}")
    private String resourceLocations;


    public Result getAllMTLX(Long dataId, String flag){
        Result<ShareTypeAndTarget> result = new Result<>();
        ShareTypeAndTarget shareTypeAndTarget = new ShareTypeAndTarget();

        //存入媒体类型数据
        shareTypeAndTarget.setList(mtlxMapper.selectAll());


        //图像
        if(StringUtils.equals(flag, "0")){
            //根据主键查询图像数据
            TImageInfo tImageInfo = imageInfoMapper.selectByPrimaryKey(dataId);

            if (tImageInfo.getTargetType()==null){
                result.setMsg("请先关联目标");
                result.setCode(500);
                return result;
            }
            //根据序号查询名称
            NewTCodeTargetType newTCodeTargetType = dmNewTargetMapper.selectByPrimaryKey(tImageInfo.getTargetType());
            if (newTCodeTargetType==null){
                result.setMsg("请先关联目标");
                result.setCode(500);
                return result;
            }

            //取得截取序号前四位
            String topXh = newTCodeTargetType.getXh().substring(0, 4);

            //通过序号查询出最顶层的名称
            String topName = dmNewTargetMapper.queryNameByXh(topXh);

            //存入目标名称
            shareTypeAndTarget.setTargetName(topName + "———" + newTCodeTargetType.getMc());
        }else{
            TVideoInfo tVideoInfo = videoInfoMapper.selectByPrimaryKey(dataId);
            if (tVideoInfo.getTargetType()==null){
                result.setMsg("请先关联目标");
                result.setCode(500);
                return result;
            }
            NewTCodeTargetType newTCodeTargetType = dmNewTargetMapper.selectByPrimaryKey(tVideoInfo.getTargetType());

            String topName = dmNewTargetMapper.queryNameByXh(newTCodeTargetType.getXh().substring(0, 4));

            //存入目标名称
            shareTypeAndTarget.setTargetName(topName + "———" + newTCodeTargetType.getMc());
        }

        result.setCode(200);
        result.setData(shareTypeAndTarget);
        return result;
    }

    public Result synchronizeData( Long dataId, String mtlxNM, String flag) throws IOException {
        /**
         * 根据媒体类型的不同，分别做不同的处理，但是会向同一个表中存储数据
         */
        ZzbzSQbMtlx MTLX = mtlxMapper.selectByPrimaryKey(mtlxNM);
        String mtlxMc = MTLX.getMc();
        QbTjwjqkWqzbMt bean=null;
        if (mtlxMc==null||mtlxMc==""){
            Result result = new Result();
            result.setCode(500);
            result.setMsg("请选择媒体类型");
            return result;
        }
        if (flag.equalsIgnoreCase("1")){
            bean=synchronizeVideo(dataId);
        }
        if (flag.equalsIgnoreCase("0")){
            bean=synchronizeImage(dataId);
        }
        /*
        设置通用属性
         */
        bean.setMtsj(new Date());
        bean.setJgnm(21+"");
        bean.setMtlxnm(mtlxNM);
        qbMapper.insert(bean);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("操作成功");
        return result;
    }

    private QbTjwjqkWqzbMt synchronizeVideo(Long dataId) throws FileNotFoundException {
        QbTjwjqkWqzbMt bean = new QbTjwjqkWqzbMt();
        TVideoInfo tVideoInfo = videoInfoMapper.selectByPrimaryKey(dataId);
        bean.setBt(tVideoInfo.getFileName());
        bean.setWjm(tVideoInfo.getFileName());
        FileInputStream fileInputStream = new FileInputStream(new File(resourceLocations + "/" + tVideoInfo.getFileUrl()));
        byte[] bytes = IoUtil.readBytes(fileInputStream);
        bean.setMtdx(bytes);
        bean.setMtcd(Long.parseLong(tVideoInfo.getFileSize().toString()));
        String fileType = tVideoInfo.getFileType();
        String otherNm = mtgsMapper.selectNmByType("其它");
        String GSNM=mtgsMapper.selectNmByType(fileType);
        if (StringUtils.isBlank(GSNM)){
            GSNM=otherNm;
        }

        bean.setMtgsnm(GSNM);
        bean.setZbnm(tVideoInfo.getTargetType());
                /*
        查询现有库中的最大值,并赋予累加值
         */
//        String utf8Str=new String(targetId.getBytes(),"UTF-8");
        String mtnt=qbMapper.getMTNM(tVideoInfo.getTargetType());
        if (mtnt==null|| mtnt.equals("")){
            mtnt="1";
        }else {
            int i = Integer.parseInt(mtnt) + 1;
            mtnt=i+"";
        }
        bean.setMtnm(mtnt);
        return bean;
    }

    private QbTjwjqkWqzbMt synchronizeImage(Long dataId) throws IOException {
        QbTjwjqkWqzbMt bean = new QbTjwjqkWqzbMt();
        TImageInfo tImageInfo = imageInfoMapper.selectByPrimaryKey(dataId);
        /*
        图像是否需要单独的标题字段
         */
        bean.setBt(tImageInfo.getFileName());
        bean.setWjm(tImageInfo.getFileName());
        FileInputStream fileInputStream = new FileInputStream(new File(resourceLocations + "/" + tImageInfo.getFileUrl()));
        int available = fileInputStream.available();
        byte[] bytes = new byte[available];
        fileInputStream.read(bytes);
        bean.setMtdx(bytes);
        bean.setMtcd(Long.parseLong(tImageInfo.getFileSize().toString()));
        String fileType = tImageInfo.getFileType();
        String otherNm = mtgsMapper.selectNmByType("其它");
        String GSNM=mtgsMapper.selectNmByType(fileType);
        if (StringUtils.isBlank(GSNM)){
            GSNM=otherNm;
        }
        bean.setMtgsnm(GSNM);
        bean.setZbnm(tImageInfo.getTargetType());
                /*
        查询现有库中的最大值,并赋予累加值
         */
//        String utf8Str=new String(targetId.getBytes(),"UTF-8");
        String mtnt=qbMapper.getMTNM(tImageInfo.getTargetType());
        if (mtnt==null|| mtnt.equals("")){
            mtnt="1";
        }else {
            int i = Integer.parseInt(mtnt) + 1;
            mtnt=i+"";
        }
        bean.setMtnm(mtnt);
        return bean;

    }

}
